package cn.iocoder.yudao.module.system.controller.admin.tenant;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantRespVO;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.type.TenantTypePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.type.TenantTypeRespVO;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.type.TenantTypeSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
import jakarta.annotation.security.PermitAll;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

import cn.iocoder.yudao.module.system.dal.dataobject.tenantType.TenantTypeDO;
import cn.iocoder.yudao.module.system.service.tenantType.TenantTypeService;

@Tag(name = "管理后台 - 商户类型")
@RestController
@RequestMapping("/system/tenant-type")
@Validated
public class TenantTypeController {

    @Resource
    private TenantTypeService tenantTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建商户类型")
    @PreAuthorize("@ss.hasPermission('system:tenant-type:create')")
    public CommonResult<Long> createTenantType(@Valid @RequestBody TenantTypeSaveReqVO createReqVO) {
        return success(tenantTypeService.createTenantType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商户类型")
    @PreAuthorize("@ss.hasPermission('system:tenant-type:update')")
    public CommonResult<Boolean> updateTenantType(@Valid @RequestBody TenantTypeSaveReqVO updateReqVO) {
        tenantTypeService.updateTenantType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商户类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:tenant-type:delete')")
    public CommonResult<Boolean> deleteTenantType(@RequestParam("id") Long id) {
        tenantTypeService.deleteTenantType(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除商户类型")
                @PreAuthorize("@ss.hasPermission('system:tenant-type:delete')")
    public CommonResult<Boolean> deleteTenantTypeList(@RequestParam("ids") List<Long> ids) {
        tenantTypeService.deleteTenantTypeListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商户类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:tenant-type:query')")
    public CommonResult<TenantTypeRespVO> getTenantType(@RequestParam("id") Long id) {
        TenantTypeDO tenantType = tenantTypeService.getTenantType(id);
        return success(BeanUtils.toBean(tenantType, TenantTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商户类型分页")
    @PreAuthorize("@ss.hasPermission('system:tenant-type:query')")
    public CommonResult<PageResult<TenantTypeRespVO>> getTenantTypePage(@Valid TenantTypePageReqVO pageReqVO) {
        PageResult<TenantTypeDO> pageResult = tenantTypeService.getTenantTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TenantTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商户类型 Excel")
    @PreAuthorize("@ss.hasPermission('system:tenant-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTenantTypeExcel(@Valid TenantTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TenantTypeDO> list = tenantTypeService.getTenantTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商户类型.xls", "数据", TenantTypeRespVO.class,
                        BeanUtils.toBean(list, TenantTypeRespVO.class));
    }

    @GetMapping({"simple-list"})
    @PermitAll
    @TenantIgnore
    @Operation(summary = "获取商户类型精简信息列表", description = "只包含被开启的商户类型，用于【添加商户】功能的选择商户类型选项")
    public CommonResult<List<TenantTypeRespVO>> getTenantTypeSimpleList() {
        List<TenantTypeDO> list = tenantTypeService.getTenantTypeListByStatus(CommonStatusEnum.ENABLE.getStatus());
        return success(convertList(list, tenantTypeDO ->
                new TenantTypeRespVO().setId(tenantTypeDO.getId()).setName(tenantTypeDO.getName())));
    }

}
